package com.ruoyi.basedata.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basedata.domain.OmsSettlementpriceentry;
import com.ruoyi.basedata.domain.BdMaterial;
import com.ruoyi.basedata.domain.OmsSettlementrateentry;
import com.ruoyi.basedata.domain.OmsSettlementrateentrytwo;
import com.ruoyi.basedata.service.IBdMaterialService;
import com.ruoyi.basedata.service.IOmsOrgunitService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basedata.domain.OmsSettlementrate;
import com.ruoyi.basedata.service.IOmsSettlementrateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 结算加成比率Controller
 * 
 * @author ptt
 * @date 2023-02-08
 */
@RestController
@RequestMapping("/settlementCenter/basedata/settlementrate")
public class OmsSettlementrateController extends BaseController
{
    @Autowired
    private IOmsSettlementrateService omsSettlementrateService;
    @Autowired
    private IOmsOrgunitService omsOrgunitService;

    @Autowired
    private IBdMaterialService bdMaterialService;

    /**
     * 查询结算加成比率列表
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basedata:settlementrate:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmsSettlementrate omsSettlementrate)
    {
        startPage();
        //逻辑修改，根据输入的条件来查询父级数据，然后将查询到的父级的id作为为参数，再次进行查询，将完整数据查询出来
        List<Long> ids = omsSettlementrateService.selectOmsSettlementrateIdList(omsSettlementrate);
        if (StringUtils.isEmpty(ids)){
            return getDataTable(new ArrayList<>());
        }
        TableDataInfo dataTable = getDataTable(ids);
//        List<OmsSettlementrate> list = omsSettlementrateService.selectOmsSettlementrateList(omsSettlementrate);
        List<OmsSettlementrate> list = omsSettlementrateService.selectOmsSettlementrateAndEntryList(ids);
        dataTable.setRows(list);
        return dataTable;
    }

    /**
     * 导出结算加成比率列表
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basedata:settlementrate:export')")
    @Log(title = "结算加成比率", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmsSettlementrate omsSettlementrate)
    {
        List<OmsSettlementrate> list = omsSettlementrateService.selectOmsSettlementrateList(omsSettlementrate);
        ExcelUtil<OmsSettlementrate> util = new ExcelUtil<OmsSettlementrate>(OmsSettlementrate.class);
        util.exportExcel(response, list, "结算加成比率数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response){
        ExcelUtil<OmsSettlementrateentry> util = new ExcelUtil<>(OmsSettlementrateentry.class);
        util.importTemplateExcel(response,"价格策略价格分录信息");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplateEntry2")
    public void importTemplateEntry2(HttpServletResponse response){
        ExcelUtil<OmsSettlementrateentrytwo> util = new ExcelUtil<>(OmsSettlementrateentrytwo.class);
        util.importTemplateExcel(response,"价格策略组织分录信息");
    }



    /**
     * 获取结算加成比率详细信息
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basedata:settlementrate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(omsSettlementrateService.selectOmsSettlementrateById(id));
    }

    /**
     * 新增结算加成比率
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basedata:settlementrate:add')")
    @Log(title = "结算加成比率", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OmsSettlementrate omsSettlementrate)
    {
        return success(omsSettlementrateService.insertOmsSettlementrate(omsSettlementrate));
    }

    /**
     * 修改结算加成比率
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basedata:settlementrate:edit')")
    @Log(title = "结算加成比率", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OmsSettlementrate omsSettlementrate)
    {
        return success(omsSettlementrateService.updateOmsSettlementrate(omsSettlementrate));
    }

    /**
     * 删除结算加成比率
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basedata:settlementrate:remove')")
    @Log(title = "结算加成比率", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(omsSettlementrateService.deleteOmsSettlementrateByIds(ids));
    }

    /**
     * 获取结算组织对象
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basedata:orgunit:query')")
    @GetMapping(value = "/omsOrg/{Number}")
    public AjaxResult queryOmsOrgObject(@PathVariable String Number)
    {
        return success(omsOrgunitService.selectOmsOrgunitByNumber(Number));
    }
    /**
     * 查询物料对象
     */
    @PreAuthorize("@ss.hasPermi('settlementCenter/basedata:settlementprice:query')")
    @Log(title = "结算价格", businessType = BusinessType.DELETE)
    @GetMapping("/material/{Number}")
    public AjaxResult queryMaterialObject(@PathVariable String Number)
    {
        BdMaterial bdMaterials = bdMaterialService.selectBdMaterialByNumber(Number);
        return success(bdMaterials);
    }
}
